package com.tvserials.notification.adapters;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.tvserials.notification.R;
import com.tvserials.notification.dto.Serial;
import com.tvserials.notification.utils.ResourceUtils;
import com.tvserials.notification.utils.VOUtils;
import com.tvserials.notification.vo.SourceType;

import java.util.List;
import java.util.Map;

public class TvSerialsExpandableListAdapter extends BaseExpandableListAdapter {

    private final SourceType sourceType;
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private Map<String, List<Serial>> _listDataChild;
    private boolean showOnlySelected = false;
    private int lastExpandedGroupPosition;
    private ExpandableListView listView;

    public TvSerialsExpandableListAdapter(Context context, List<String> listDataHeader,
                                          Map<String, List<Serial>> listChildData,
                                          SourceType sourceType) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
        this.sourceType = sourceType;
    }

    public void setShowOnlySelected(boolean showOnlySelected) {
        this.showOnlySelected = showOnlySelected;
    }

    public boolean isShowOnlySelected() {
        return showOnlySelected;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<Serial> serials = this._listDataChild.get(this._listDataHeader.get(groupPosition));
        /*if (showOnlySelected) {
            List<Serial> selectedItems = VOUtils.getSelectedItems(serials);
            return (selectedItems.size() > childPosition) ? selectedItems.get(childPosition) :
                    serials.get(childPosition);
        }*/
        return serials.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        listView = (ExpandableListView) parent;
        final Serial serial = (Serial) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.serials_list_item, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.serialsIcon);
            viewHolder.checkedTextView = (CheckedTextView) convertView.findViewById(R.id.checkText);
            viewHolder.checkedTextView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    CheckedTextView checkedTextView = (CheckedTextView) v;
                    checkedTextView.setChecked(!checkedTextView.isChecked());
                    Serial element = (Serial) v.getTag();
                    element.setSelected(checkedTextView.isChecked());
                }
            });
            viewHolder.checkedTextView.setTag(serial);
            convertView.setTag(viewHolder);
        } else {
            ((ViewHolder) convertView.getTag()).checkedTextView.setTag(serial);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.checkedTextView.setText(serial.getShowName());
        holder.checkedTextView.setChecked(serial.isSelected());
        holder.imageView.setImageResource(ResourceUtils.getIconResourceId(serial.getSourceType()));

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<Serial> serials = this._listDataChild.get(this._listDataHeader.get(groupPosition));
        if (showOnlySelected) {
            return VOUtils.getSelectedItems(serials).size();
        }
        return serials.size();
    }

    public int getChildsCount(int groupPosition) {
        List<Serial> serials = this._listDataChild.get(this._listDataHeader.get(groupPosition));
        return serials.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        listView = (ExpandableListView) parent;
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public List<Serial> getSelectedItems() {
        return VOUtils.getSelectedItems(_listDataChild);
    }

    public void scrollToItem(String newText) {
        if (newText.isEmpty()) {
            listView.setSelection(0);
            return;
        }
        String firstItem = String.valueOf(newText.charAt(0)).toLowerCase();
        int groupPosition = 0;
        groupPosition = getGroupPosition(firstItem);
        collapseAllGroups();
        listView.expandGroup(groupPosition);
        int childPosition = getChildPosition(groupPosition, newText);
        listView.setSelection(groupPosition + childPosition + 1);
    }

    private int getChildPosition(int groupPosition, String newText) {
        List<Serial> serials = _listDataChild.get(_listDataHeader.get(groupPosition));
        for (int i = 0; i < serials.size(); i++) {
            Serial serial = serials.get(i);
            if (serial.getShowName().toLowerCase().startsWith(newText.toLowerCase())) {
                return i;
            }
        }
        return 0;
    }

    private int getGroupPosition(String firstItem) {
        int groupPosition = 0;
        for (int i = 0; i < _listDataHeader.size(); i++) {
            String headerName = _listDataHeader.get(i).toLowerCase();
            if (headerName.equals(firstItem)) {
                return i;
            }
        }
        return groupPosition;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        //collapse the old expanded group, if not the same
        //as new group to expand
//        if (groupPosition != lastExpandedGroupPosition) {
//            listView.collapseGroup(lastExpandedGroupPosition);
//        }

        super.onGroupExpanded(groupPosition);
        lastExpandedGroupPosition = groupPosition;
    }

    public void expandAllGroups() {
        for (int i = 0; i < _listDataHeader.size(); i++) {
            listView.expandGroup(i);
        }
    }

    public void collapseAllGroups() {
        for (int i = 0; i < _listDataHeader.size(); i++) {
            listView.collapseGroup(i);
        }
    }

    private static class ViewHolder {
        public CheckedTextView checkedTextView;
        public ImageView imageView;
    }
}